﻿/* ===============================================================================  
 * Copyright (c) 2010 Acid Frameworks
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * =============================================================================== */

using System;
using System.Collections.Generic;
using System.Drawing;
using Acid.Graphics;

namespace Acid.Rendering
{
    /// <summary>
    /// A TextureRenderNode is used to output the results of a rendering pass to a Target Texture2D. 
    /// </summary>
    public class RenderToTextureNode : RenderNode , IDisposable
    {
        /// <summary>
        /// The FrameBuffer Object for this RenderToTextureNode.
        /// </summary>
        public FrameBuffer FrameBuffer   { get; set; }

        /// <summary>
        /// The DepthBuffer Object for this RenderToTextureNode.
        /// </summary>
        public DepthBuffer DepthBuffer   { get; set; }

        /// <summary>
        /// The Target Texture2D
        /// </summary>
        public Texture2D   Target        { get; set; }

        /// <summary>
        /// Creates this Render to Texture Node without a DepthBuffer
        /// </summary>
        /// <param name="device"></param>
        public RenderToTextureNode(Device device) : base(device)
        {
            this.FrameBuffer = new FrameBuffer(device);

            this.Target = null;
        }
        /// <summary>
        /// Creates this Render to Texture Node with a DepthBuffer
        /// </summary>
        /// <param name="device"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="depth"></param>
        public RenderToTextureNode(Device device, int width, int height, Depth depth) : base(device)
        {
            this.FrameBuffer = new FrameBuffer(device);

            this.DepthBuffer = new DepthBuffer(device, width, height, depth);
            
            this.Target      = null;
        }
 
        #region IDisposable Members

        private bool disposed = false;

        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (this.FrameBuffer != null)
                    {
                        this.FrameBuffer.Dispose();
                    }

                    if (this.DepthBuffer != null)
                    {
                        this.DepthBuffer.Dispose();
                    }
                }

                this.disposed = true;
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        ~RenderToTextureNode()
        {
            this.Dispose(false);
        }

        #endregion
    }
    /// <summary>
    /// A RenderNode is the primary entry point for rendering in the Graph. It is responsible for clearing the frame, setting the viewport, and beginning the rendering process.
    /// A RenderNode often is part of a tree of RenderNodes. During a rendering pass, a Graphs RenderNode is visited first, and is depth first traverse to child renderNodes. RenderNodes
    /// also support ordering with the Priority Property. 
    /// </summary>
    public class RenderNode : IPackObject
    {
        private static int Count { get; set; }

        /// <summary>
        /// This RenderNodes Rendering Device
        /// </summary>
        public Device           Device    { get; set; }
        /// <summary>
        /// The ViewInfo for this RenderNode
        /// </summary>
        public ViewInfo         ViewInfo  { get; set; }
        /// <summary>
        /// Active specifies if this RenderNode is to take part in the rendering pass. Default is true.
        /// </summary>
        public bool             Active    { get; set; }
        /// <summary>
        /// This RenderNodes Children.
        /// </summary>
        public List<RenderNode> RenderNodes  { get; set; }
        /// <summary>
        /// The ClearColor for this RenderNode. 
        /// </summary>
        public Color            ClearColor{ get; set; }
        /// <summary>
        /// The ViewPort for this RenderNode. 
        /// </summary>
        public ViewPort         ViewPort  { get; set; }
        /// <summary>
        /// The Priority of this RenderNode in relation to its siblings. Default is 1 
        /// </summary>
        public int              Priority  { get; set; }
        
        public RenderNode(Device device)
        {
            this.ID         = "renderNode_" + RenderNode.Count.ToString(); RenderNode.Count++;
            this.Device     = device;
            this.ViewInfo   = null;
            this.Active     = true;
            this.RenderNodes   = new List<RenderNode>();
            this.ClearColor = Color.Black;
            this.Priority   = 0;
            this.ViewPort   = new ViewPort(0, 0, 512, 512);
        }


        #region IGraphObject Members

        /// <summary>
        /// This RenderNodes ID
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// This RenderNodes Name
        /// </summary>
        public string Name { get; set; }
        #endregion
    }
}
